package polimi.sc.expoquest.screen.gossip;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import polimi.sc.expoquest.QuestApplication;
import polimi.sc.expoquest.R;
import polimi.sc.expoquest.screen.DashboardActivity;
import polimi.sc.expoquest.storage.datasources.GossipCommentDataSource;
import polimi.sc.expoquest.storage.datasources.GossipDataSource;
import polimi.sc.expoquest.storage.model.Gossip;
import polimi.sc.expoquest.storage.model.GossipComment;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.support.v4.app.NavUtils;


public class MyGossipsActivity extends Activity {
	
	public TabHost tabHost;
	
	private GossipDataSource GossipDS;
	private GossipCommentDataSource GossipCommentDS;
	
	public ListView listView;
	public List<Gossip> gossipObjects;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_gossips);
		
	//	getActionBar().setDisplayHomeAsUpEnabled(true);
    gossipObjects = new ArrayList<Gossip>();	
	//create tabs	
	tabHost=(TabHost)findViewById(R.id.tabhost);
	tabHost.setup();		
		
		TabSpec spec1=tabHost.newTabSpec("My Comments");
		spec1.setIndicator("My Comments");		
		spec1.setContent(R.id.tab1);	
		
		TabSpec spec2=tabHost.newTabSpec("My Topics");
		spec2.setIndicator("My Topics");
		spec2.setContent(R.id.tab2);		
				
		tabHost.addTab(spec1);
		tabHost.addTab(spec2);
		
	//creat listener
		 tabHost.getTabWidget().getChildAt(0).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							
							goMyComment();
							
						}
					});
		 
		 tabHost.getTabWidget().getChildAt(1).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							
							goMyTopic();
						}
					});
	//find the listview
		 listView = (ListView) findViewById(R.id.gossiplist);
    //listener for listview
		 listView.setOnItemClickListener(
				 new OnItemClickListener() {			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				// arg3 is which row(row id) is clicked
				
				checkgossip(arg3);
			}
			});
    //go my comments
		 goMyComment();
		
					
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_my_gossips, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void homepagebtn_click(View v){
		  Intent dashbordIntent = new Intent(this, DashboardActivity.class);
		        startActivity(dashbordIntent);
		 }
	public void goMyComment()
	{
		//change the tab
		tabHost.getTabWidget().setCurrentTab(0);
		//get the user
		QuestApplication questApp = (QuestApplication)getApplicationContext();
		int userID = questApp.getUserID();
		
		//get my comment
		if(GossipCommentDS== null)
			GossipCommentDS = new GossipCommentDataSource(this);
		GossipCommentDS.open();
		Collection<GossipComment> GossipCommentlist= GossipCommentDS.getAllGossipsCommentsForUser(userID);
		 //clear the object
		GossipCommentDS.close();
		
		gossipObjects.clear();
		List<String> gossipcontent=new ArrayList<String>();
		for(GossipComment oneGossip:GossipCommentlist)
		{
			//get the gossip accordingly	
			gossipObjects.add(oneGossip.getGossip());
			//set the list
			gossipcontent.add(oneGossip.getComment());
		}
		//set the adapter
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.customer_listview, gossipcontent);
		listView.setAdapter(adapter); 
		 
	}
	public void goMyTopic()
	{
		//change the tab
		tabHost.getTabWidget().setCurrentTab(1);
		//get the user
		QuestApplication questApp = (QuestApplication)getApplicationContext();
		int userID = questApp.getUserID();
		//get my topic
		if(GossipDS== null)
			GossipDS = new GossipDataSource(this);    
		GossipDS.open();		
		Collection<Gossip> Gossiplist= GossipDS.getGossipsForUser(userID);
		GossipDS.close(); 
		
		//clear the object
		gossipObjects.clear();
			 
		List<String> gossipcontent=new ArrayList<String>();
		for(Gossip oneGossip:Gossiplist)
		{
			//set the object
			gossipObjects.add(oneGossip);
			//set the list	
			gossipcontent.add(oneGossip.getGossip());
		}
		//set the adapter
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.customer_listview, gossipcontent);
		listView.setAdapter(adapter); 
	}
	public void checkgossip(long GossipID)
	{
		Intent newGossipIntent = new Intent(this, GossipMainActivity.class);
		int selectedID=gossipObjects.get((int) (GossipID)).getId();
		newGossipIntent.putExtra("DELIVERDGOSSIP", selectedID);
        startActivity(newGossipIntent);
        
	}
	
}
